// pages/orderform/index.js
const app = getApp();
const api = require("../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    data:{
      number:'',
      addresss:'',
      names:''
    },
    img:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.click();     //执行获取位置
  },
  //下单
  subbtn(){
    let that = this;
    var data = that.data.data;
    console.log(data)
    if (data.number == '1'){
      app.showToast('请拍车牌号',0)
      return false;
    } else if (data.addresss == '1') {
      app.showToast('请输入目的地', 0);
      return false;
    }else{
      let request = {
        startAddress: {
          address: data.name,     //data.address + 
          location: {
            lat: data.lat,
            lng: data.lng
          }
        },
        endAddress: {
          address: data.names,    //data.addresss + 
          location: {
            lat: data.lats,
            lng: data.lngs
          }
        },
        card_number: data.number
      }
      app.Request({ url: 'driver_order/place_an_order', method: 'post', data: request }).then((res)=>{
        if(res.code == 1){
          app.showToast(res.msg, 0,1500,function(){
            app.navTo('/pages/driverOrder/orderedStatus/orderedStatus?order_no=' + res.data.order_no)
          })
        }else{
          app.showToast(res.msg, 0)
        }
      })
    }
    
  },
  address(){
    let that = this;
    var data = that.data.data;
    wx.chooseLocation({
      success(res){
        data.addresss = res.address;
        data.names = res.name;
        data.lats = res.latitude;
        data.lngs = res.longitude;
        that.setData({
          data:data
        })
      },
      fail(err){
        app.showToast('打开地图失败',0)
      }
    })
  },
  takePhoto(){
    let that = this;
    const ctx = wx.createCameraContext()
    wx.showLoading({
      title: '识别中',
    })
    var data = that.data.data;
    ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log(res)
        wx.saveImageToPhotosAlbum({
          filePath: res.tempImagePath,
          success: function (success) {
            console.log(success)
            console.log('保存成功，进入选择')
            wx.chooseImage({
              count:1,
              type: 'image',
              sourceType: ['album'],
              success(ress) {
                wx.uploadFile({
                  url: api.url + 'driver_order/plate_ocr' + '&_acid=' + app.siteInfo.acid + '&access_token=' + wx.getStorageSync("access_token"),
                  filePath: ress.tempFilePaths[0],
                  name: 'file',
                  success: (resss) => {
                    var datas = JSON.parse(resss.data)
                    wx.hideLoading();
                    if (datas.code == 1){
                      data.number = datas.data.number;
                      that.setData({
                        data: data
                      })
                    }else{
                      app.showToast(datas.msg,0)
                    }
                    
                  }
                })
              }
            })
          },
          file(err){
            console.log(err)
          }
        })
        // wx.downloadFile({
        //   url: res.tempImagePath, //仅为示例，并非真实的资源
        //   success(res) {
        //     console.log(res)
        //   }
        // })
        
        // that.setData({
        //   img: res.tempImagePath,
        //   data:data
        // })
      }
    })
  },
  click(){
    let that = this;
    var data = that.data.data;
    wx.getLocation({
      success: function (res) {
        app.reverseGeocoder({lat:res.latitude,lng:res.longitude,success(res){
          console.log(res)
          data.address = res.result.pois[0].address;
          data.name = res.result.pois[0].title;
          data.lat = res.result.pois[0].location.lat;
          data.lng = res.result.pois[0].location.lng;
          that.setData({
            data: data
          })
          console.log(that.data.data)
        }})
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})